/**
 * Copyright (C) 2016 Fuzhou Rockchip Electronics Co., Ltd
 * author: Neil Lin <neil.lin@rock-chips.com>
 *         Benjo Lei <benjo.lei@rock-chips.com>
 *         Jinkun Hong <hjk@rock-chips.com>
 *         Chris Zhong <zyw@rock-chips.com>
 *
 * This software is licensed under the terms of the GNU General Public
 * License version 2, as published by the Free Software Foundation, and
 * may be copied, distributed, and modified under those terms.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include "menu_commond.h"

#include <assert.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>

#include <sys/stat.h>

#include "camera_ui.h"
#include "camera_ui_def.h"
#include "storage/storage.h"
#include "video.h"

#define hwnd_for_menu_command 0

int setting_func_recovery_ui(void)
{
	if (!FlagForUI_ca.video_rec_state_ui) {
		printf("setting_func_recovery\n");
		parameter_recover();
		power_reboot();
		return 1;
	}

	return 0;
}

int setting_func_USBMODE_ui(char *str)
{
	if (!FlagForUI_ca.video_rec_state_ui) {
		if (!strcmp(str, "msc")) {
			printf("SET usb mode msc\n");
			parameter_save_video_usb(0);
			android_usb_config_ums();
		} else if (!strcmp(str, "adb")) {
			printf("SET usb mode adb\n");
			parameter_save_video_usb(1);
			android_usb_config_adb();
		}

		return 1;
	}

	return 0;
}

int setting_func_phtomp_ui(char *str)
{
	bool restart_rec = FlagForUI_ca.video_rec_state_ui;
	int resolution = 0;

	if (!strcmp(str, "1m"))
		resolution = 0;
	else if (!strcmp(str, "2m"))
		resolution = 1;
	else if (!strcmp(str, "3m"))
		resolution = 2;
	else
		return -1;

	if (restart_rec)
		stoprec(NULL);

	parameter_save_vcamresolution_photo(resolution);
	ui_deinit_init_camera(hwnd_for_menu_command);

	if (restart_rec)
		startrec(NULL);

	return 0;
}

int setting_func_video_length_ui(char *str)
{
	bool restart_rec = FlagForUI_ca.video_rec_state_ui;
	int time = 0;

	if (!strcmp(str, "1min"))
		time = 60;
	else if (!strcmp(str, "3min"))
		time = 180;
	else if (!strcmp(str, "5min"))
		time = 300;
	else
		return -1;

	if (restart_rec)
		stoprec(NULL);

	parameter_save_recodetime(time);
	storage_setting_event_callback(0, NULL, NULL);

	if (restart_rec)
		startrec(NULL);

	return 0;
}

int setting_func_3dnr_ui(char *str)
{
	bool restart_rec = FlagForUI_ca.video_rec_state_ui;
	bool en_3dnr = false;

	if (!strcmp(str, "ON"))
		en_3dnr = true;
	else if (!strcmp(str, "OFF"))
		en_3dnr = false;
	else
		return -1;

	if (restart_rec)
		stoprec(NULL);

	parameter_save_video_3dnr(en_3dnr);
	ui_deinit_init_camera(hwnd_for_menu_command);

	if (restart_rec)
		startrec(NULL);

	return 0;
}

int get_format_status(void)
{
	return FlagForUI_ca.formatflag;
}

static int get_mode_ui(void)
{
	return FlagForUI_ca.setmode_ui;
}

int get_src_state(void)
{
	return FlagForUI_ca.video_rec_state_ui;
}

int setting_func_format_ui(void (*p)(void))
{
	if (FlagForUI_ca.video_rec_state_ui)
		stoprec(NULL);

	wififormat_callback = p;

	if (!FlagForUI_ca.formatflag)
		fs_manage_format_sdcard(sdcardformat_back,
					(void *)NULL, 0);

	return 0;
}

int setting_func_ldw_ui(char *str)
{
	bool restart_rec = FlagForUI_ca.video_rec_state_ui;
#ifdef NIQITEST
	if (restart_rec)
		stoprec(NULL);

	if (!strcmp(str, "ON")) {
		parameter_save_video_ldw(1);
		ui_deinit_init_camera(hwnd_for_menu_command);
	} else if (!strcmp(str, "OFF")) {
		parameter_save_video_ldw(0);
		ui_deinit_init_camera(hwnd_for_menu_command);
		FlagForUI_ca.adasflagtooff = 1;
	}

	if (restart_rec)
		startrec(NULL);
#endif
	return 0;
}

int setting_func_white_balance_ui(char *str)
{
	bool restart_rec = FlagForUI_ca.video_rec_state_ui;
	int wb = 0;

	if (!strcmp(str, "auto"))
		wb = 0;
	else if (!strcmp(str, "Daylight"))
		wb = 1;
	else if (!strcmp(str, "fluocrescence"))
		wb = 2;
	else if (!strcmp(str, "cloudysky"))
		wb = 3;
	else if (!strcmp(str, "tungsten"))
		wb = 4;
	else
		return -1;

	if (restart_rec)
		stoprec(NULL);

	ui_set_white_balance(wb);

	if (restart_rec)
		startrec(NULL);

	return 0;
}

int setting_func_exposure_ui(char *str)
{
	bool restart_rec = FlagForUI_ca.video_rec_state_ui;
	int ec = 0;

	if (!strcmp(str, "-3"))
		ec = 0;
	else if (!strcmp(str, "-2"))
		ec = 1;
	else if (!strcmp(str, "-1"))
		ec = 2;
	else if (!strcmp(str, "0"))
		ec = 3;
	else if (!strcmp(str, "1"))
		ec = 4;
	else
		return -1;

	if (restart_rec)
		stoprec(NULL);

	ui_set_exposure_compensation(ec);

	if (restart_rec)
		startrec(NULL);

	return 0;
}

int setting_func_data_stamp_ui(char *str)
{
	bool restart_rec = FlagForUI_ca.video_rec_state_ui;
	bool mark_en = false;

	if (!strcmp(str, "ON"))
		mark_en = true;
	else if (!strcmp(str, "OFF"))
		mark_en = false;
	else
		return -1;

	if (restart_rec)
		stoprec(NULL);

	parameter_save_video_mark(mark_en);

	if (restart_rec)
		startrec(NULL);

	return 0;
}

int setting_func_record_audio_ui(char *str)
{
	bool restart_rec = FlagForUI_ca.video_rec_state_ui;
	bool mic_en = false;

	if (!strcmp(str, "ON"))
		mic_en = true;
	else if (!strcmp(str, "OFF"))
		mic_en = false;
	else
		return -1;

	if (restart_rec)
		stoprec(NULL);

	mic_onoff(NULL, mic_en);

	if (restart_rec)
		startrec(NULL);

	return 0;
}

int setting_func_language_ui(char *str)
{
	bool restart_rec = FlagForUI_ca.video_rec_state_ui;
	bool chinese_en = false;

	if (!strcmp(str, "English"))
		chinese_en = false;
	else if (!strcmp(str, "Chinese"))
		chinese_en = true;
	else
		return -1;

	if (restart_rec)
		stoprec(NULL);

	parameter_save_video_lan(chinese_en);

	if (restart_rec)
		startrec(NULL);

	return 0;
}

int setting_func_backlight_lcd_ui(char *str)
{
	bool restart_rec = FlagForUI_ca.video_rec_state_ui;
	int level = LCD_BACKLT_H;
#ifdef NIQITEST
	if (!strcmp(str, "high"))
		level = LCD_BACKLT_H;
	else if (!strcmp(str, "mid"))
		level = LCD_BACKLT_M;
	else if (!strcmp(str, "low"))
		level = LCD_BACKLT_L;
	else
		return -1;

	if (restart_rec)
		stoprec(NULL);

	parameter_save_video_backlt(level);
	rk_fb_set_lcd_backlight(level);

	if (restart_rec)
		startrec(NULL);
#endif
	return 0;
}

int setting_func_setdata_ui(char *str)
{
	bool restart_rec = FlagForUI_ca.video_rec_state_ui;
	unsigned int year, mon, mday, hour, min, sec;
	struct tm tm;
	int ret;

	ret = sscanf(str, "%u-%u-%u %u:%u:%u",
		     &year, &mon, &mday, &hour, &min, &sec);
	if (ret != 6) {
		printf("Couldn't process date: %s\n", str);
		return -1;
	}

	if (restart_rec)
		stoprec(NULL);

	tm.tm_year = year - 1900;
	tm.tm_mon = mon - 1;
	tm.tm_mday = mday;
	tm.tm_hour = hour;
	tm.tm_min = min;
	tm.tm_sec = sec;
	setDateTime(&tm);

	if (restart_rec)
		startrec(NULL);

	return 0;
}

int setting_func_frequency_ui(char *str)
{
	bool restart_rec = FlagForUI_ca.video_rec_state_ui;
	char freq = CAMARE_FREQ_50HZ;

	if (!strcmp(str, "50HZ"))
		freq = CAMARE_FREQ_50HZ;
	else if (!strcmp(str, "60HZ"))
		freq = CAMARE_FREQ_60HZ;
	else
		return -1;

	if (restart_rec)
		stoprec(NULL);

	cmd_IDM_frequency(freq);

	if (restart_rec)
		startrec(NULL);

	return 0;
}

int setting_func_autorecord_ui(char *str)
{
	bool restart_rec = FlagForUI_ca.video_rec_state_ui;
	bool auto_rec_en = false;
#ifdef NIQITEST
	if (!strcmp(str, "ON"))
		auto_rec_en = true;
	else if (!strcmp(str, "OFF"))
		auto_rec_en = false;
	else
		return -1;

	if (restart_rec)
		stoprec(NULL);

	parameter_save_video_autorec(auto_rec_en);

	if (restart_rec)
		startrec(NULL);
#endif
	return 0;
}

int setting_func_car_ui(char *str)
{
	bool restart_rec = FlagForUI_ca.video_rec_state_ui;
	char camera = 0;
	return 0;
#ifdef NIQITEST
	if (!strcmp(str, "Front"))
		camera = 0;
	else if (!strcmp(str, "Rear"))
		camera = 1;
	else if (!strcmp(str, "Double"))
		camera = 2;
	else 

	if (restart_rec)
		stoprec(NULL);

	cmd_IDM_CAR(NULL, camera);

	if (restart_rec)
		startrec(NULL);
#endif
	return 0;
}

int setting_func_modechage_ui(char *str)
{
	if (!strcmp(str, "photo") && !get_mode_ui())
		changemode(NULL, 1);
	else if (!strcmp(str, "recording") && get_mode_ui())
		changemode(NULL, 0);

	return 0;
}

int setting_func_rec_ui(char *str)
{
	bool start_rec = false;

	/* if no sdcard, no need change anything here */
	if (!FlagForUI_ca.sdcard_ui)
		return 0;

	if (!strcmp(str, "on"))
		start_rec = true;
	else if (!strcmp(str, "off"))
		start_rec = false;
	else
		return -1;

	/* if already on or already off, no need turn on/off again */
	if (!(start_rec ^ FlagForUI_ca.video_rec_state_ui))
		return 0;

	if (start_rec)
		startrec(NULL);
	else
		stoprec(NULL);

	return 1;
}

int setting_func_videoquality_ui(char *str)
{
    unsigned int level = 0;
#ifdef NIQITEST
    if (!strcmp(str, "low"))
        level = VIDEO_QUALITY_LOW;
    else if (!strcmp(str, "middle"))
        level = VIDEO_QUALITY_MID;
    else if (!strcmp(str, "high"))
        level = VIDEO_QUALITY_HIGH;
    else
        return -1;

    cmd_IDM_VIDEO_QUALITY(hwnd_for_menu_command, level);
#endif    
    return 0;
}

int setting_func_reboot_ui(char *str)
{
	if (FlagForUI_ca.video_rec_state_ui)
		stoprec(NULL);

	if (!strcmp(str, "off"))
		parameter_save_debug_reboot(0);
	else if (!strcmp(str, "on"))
		parameter_save_debug_reboot(1);

	return 0;
}

int get_reboot_parameter(void)
{
	return parameter_get_debug_reboot();
}

void save_reboot_parameter(int param)
{
	parameter_save_debug_reboot(param);
}

int setting_func_recover_ui(char *str)
{
	if (FlagForUI_ca.video_rec_state_ui)
		stoprec(NULL);

	if (!strcmp(str, "off"))
		parameter_save_debug_recovery(0);
	else if (!strcmp(str, "on"))
		parameter_save_debug_recovery(1);

	return 0;
}

int get_recover_parameter(void)
{
	return parameter_get_debug_recovery();
}

void save_recover_parameter(int param)
{
	parameter_save_debug_recovery(param);
}

int setting_func_awake_1_ui(char *str)
{
	if (FlagForUI_ca.video_rec_state_ui)
		stoprec(NULL);

	if (!strcmp(str, "off"))
		parameter_save_debug_awake(0);
	else if (!strcmp(str, "on"))
		parameter_save_debug_awake(1);

	return 0;
}

int get_awake_parameter(void)
{
	return parameter_get_debug_awake();
}

void save_awake_parameter(int param)
{
	parameter_save_debug_awake(param);
}

int setting_func_standby_ui(char *str)
{
	if (FlagForUI_ca.video_rec_state_ui)
		stoprec(NULL);

	if (!strcmp(str, "off"))
		parameter_save_debug_standby(0);
	else if (!strcmp(str, "on"))
		parameter_save_debug_standby(1);

	return 0;
}

int get_standby_parameter(void)
{
	return parameter_get_debug_standby();
}

void save_standby_parameter(int param)
{
	parameter_save_debug_standby(param);
}

int setting_func_modechange_ui(char *str)
{
	if (FlagForUI_ca.video_rec_state_ui)
		stoprec(NULL);

	if (!strcmp(str, "off"))
		parameter_save_debug_modechange(0);
	else if (!strcmp(str, "on"))
		parameter_save_debug_modechange(1);

	return 0;
}

int get_mode_change_parameter(void)
{
	return parameter_get_debug_modechange();
}

void save_mode_change_parameter(int param)
{
	parameter_save_debug_modechange(param);
}

int setting_func_video_ui(char *str)
{
	if (FlagForUI_ca.video_rec_state_ui)
		stoprec(NULL);

	if (!strcmp(str, "off"))
		parameter_save_debug_video(0);
	else if (!strcmp(str, "on"))
		parameter_save_debug_video(1);

	return 0;
}

int get_video_parameter(void)
{
	return parameter_get_debug_video();
}

void save_video_parameter(int param)
{
	parameter_save_debug_video(param);
}

int setting_func_begin_end_video_ui(char *str)
{
	if (FlagForUI_ca.video_rec_state_ui)
		stoprec(NULL);

	if (!strcmp(str, "off"))
		parameter_save_debug_beg_end_video(0);
	else if (!strcmp(str, "on"))
		parameter_save_debug_beg_end_video(1);

	return 0;
}

int get_beg_end_video_parameter(void)
{
	return parameter_get_debug_beg_end_video();
}

void save_beg_end_video_parameter(int param)
{
	parameter_save_debug_beg_end_video(param);
}

int setting_func_photo_ui(char *str)
{
	if (FlagForUI_ca.video_rec_state_ui)
		stoprec(NULL);

	if (!strcmp(str, "off"))
		parameter_save_debug_photo(0);
	else if (!strcmp(str, "on"))
		parameter_save_debug_photo(1);

	return 0;
}

int get_photo_parameter(void)
{
	return parameter_get_debug_photo();
}

void save_photo_parameter(int param)
{
	parameter_save_debug_photo(param);
}

int setting_func_temp_ui(char *str)
{
	if (FlagForUI_ca.video_rec_state_ui)
		stoprec(NULL);

	if (!strcmp(str, "off"))
		parameter_save_debug_temp(0);
	else if (!strcmp(str, "on"))
		parameter_save_debug_temp(1);

	return 0;
}

int get_temp_parameter(void)
{
	return parameter_get_debug_temp();
}

void save_temp_parameter(int param)
{
	parameter_save_debug_temp(param);
}

int setting_func_videobitrate_ui(char *str)
{
	int rate = 0;

	if (!strcmp(str, "1"))
		rate = 1;
	else if (!strcmp(str, "2"))
		rate = 2;
	else if (!strcmp(str, "4"))
		rate = 3;
	else if (!strcmp(str, "6"))
		rate = 4;
	else if (!strcmp(str, "8"))
		rate = 5;
	else if (!strcmp(str, "10"))
		rate = 6;
	else if (!strcmp(str, "12"))
		rate = 7;
	else
		return -1;

	cmd_IDM_DEBUG_VIDEO_BIT_RATE(NULL, rate);

	return 0;
}

int get_video_bitrate_parameter(void)
{
	return parameter_get_bit_rate_per_pixel();
}

void save_video_bitrate_parameter(int param)
{
	parameter_save_bit_rate_per_pixel(param);
}
